package com.dingshi.security.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.R;
import com.dingshi.security.RepairHistoryActivity;
import com.dingshi.security.entity.Track;
import com.dingshi.security.util.Utils;

public class HistoryAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context context;
	private ArrayList<Track> list;
	public static int type_service = 0;
	public static int type_alarm_service = 1;
	private int this_type = type_alarm_service;

	public HistoryAdapter(Context context, ArrayList<Track> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setType(int type) {
		this_type = type;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Track track = list.get(position);
		if (track == null) {
			return null;
		}
		final ViewHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.history_item, null);
			holder = new ViewHolder();
			holder.historyItem = (LinearLayout) convertView
					.findViewById(R.id.history_item);
			holder.devicename_l1 = (TextView) convertView
					.findViewById(R.id.devicename);
			holder.time_l2 = (TextView) convertView
					.findViewById(R.id.timestamp);
			holder.status_l3 = (TextView) convertView.findViewById(R.id.status);
			holder.onduty_l4 = (TextView) convertView.findViewById(R.id.onduty);
			holder.historyItem.setId(position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		updateData(holder, track);

		OnClickListener clickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, RepairHistoryActivity.class);
				intent.putExtra("oddNum", list.get(v.getId()).getOddNum());
				context.startActivity(intent);
			}
		};
		holder.historyItem.setOnClickListener(clickListener);
		return convertView;
	}

	private void updateData(ViewHolder holder, Track track) {
		if (track != null) {
			holder.devicename_l1.setText(track.getDeviceName());
			String start_time = track.getReportDate();
			if(!Utils.isNullOrEmpty(start_time)) {
				String[] split = start_time.split(" ");
				holder.time_l2.setText(split[0]);
			}else {
				holder.time_l2.setText(track.getReportDate());
			}
			String end_time = track.getFinishDate();
			if(!Utils.isNullOrEmpty(end_time)) {
				String[] split = end_time.split(" ");
				holder.status_l3.setText(split[0]);
			}else {
				holder.status_l3.setText("未完毕");
			}
			if (this_type == type_alarm_service) {
				holder.onduty_l4.setText(track.getName());
			} else if (this_type == type_service) {
				holder.onduty_l4.setText(track.getArea());
			}

		}
	}

	static class ViewHolder {
		public LinearLayout historyItem;
		public TextView devicename_l1;
		public TextView time_l2;
		public TextView status_l3;
		public TextView onduty_l4;
	}
}
